package cn.hamster3.mc.plugin.ball.bukkit.api;

import cn.hamster3.mc.plugin.ball.bukkit.HamsterBallPlugin;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Logger;

public class BallBukkitAPI extends BallAPI {
    public BallBukkitAPI(@NotNull ConfigSection config) {
        super(config, BallServerType.GAME);
    }

    public static BallBukkitAPI getInstance() {
        return (BallBukkitAPI) instance;
    }

    public static void init(@NotNull File configFile) throws IOException {
        if (instance != null) {
            return;
        }
        YamlConfig config = YamlConfig.load(configFile);
        instance = new BallBukkitAPI(config);
    }

    @Override
    public void enable() throws SQLException, InterruptedException {
        instance.getLocalServerInfo().setHost(Bukkit.getIp().isEmpty() ? "127.0.0.1" : Bukkit.getIp());
        instance.getLocalServerInfo().setPort(Bukkit.getPort());
        super.enable();
    }

    @Override
    public void disable() throws SQLException, InterruptedException {
        super.disable();
    }

    @Override
    public @NotNull Logger getLogger() {
        return HamsterBallPlugin.getInstance().getLogger();
    }

}
